//
//  UserViewController.m
//  reader
//
//  Created by mo on 2021/12/12.
//  Copyright © 2021 mo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "UserViewController.h"
#import "InfoViewController.h"
#import "CollectionCell.h"
#import "Masonry.h"

@interface UserViewController()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UIImage* userImg;
@property (nonatomic, strong) NSString* userName;
@property (nonatomic, strong) NSString* userSign;
@end


@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    _userImg = [UIImage imageNamed:@"default"];
    _userName = @"未登录";
    _userSign = @"输入个性签名";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"grayBack"]];
    [self addInfoCollectionView];
    [self addTableView];
}

- (void)setName:(NSString*)userName{
    _userName = userName;
    [self.collectionView reloadData];
}
- (void)setImg:(UIImage*)img{
    _userImg = img;
    [self.collectionView reloadData];
}
- (void)setSign:(NSString*)sign{
    _userSign = sign;
    [self.collectionView reloadData];
}

- (void) addInfoCollectionView {
    // 创建 layout, 设置布局方向为垂直
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 设置每个 item 的大小
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    layout.itemSize = CGSizeMake(screenBounds.size.width, screenBounds.size.height/10);
    
    // 通过布局策略 layout 创建 collectionView
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,0,0) collectionViewLayout:layout];
    // 注册Cell
    [_collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cellId"];
    //设置背景
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //添加代理
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    
    [self.view addSubview:_collectionView];
    
    //设置约束
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(screenBounds.size.width,screenBounds.size.height/7));
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top).offset(screenBounds.size.height/10);
    }];
}

//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

//UICollectionView 代理方法
- (CollectionCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.view.image = _userImg;
    cell.name.text = _userName;
    cell.sign.text = _userSign;
    
    return cell;
}

//设置cell间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 0, 10, 0); //上、左、下、右
}

//选择跳转
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //设置返回按钮
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    //页面跳转
    InfoViewController *next = [[InfoViewController alloc]init];
    [next setDelegate:self];
    [next setName:_userName withImg:_userImg withSign:_userSign];
    [self.navigationController pushViewController:next animated:true];
}

- (void) addTableView {
    _cellLabels = [NSArray arrayWithObjects:@"设置",@"缓存管理",@"问题反馈",@"关于", nil];
    _cellImages = [NSArray arrayWithObjects:@"setting", @"delete",@"advice",@"about",nil];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    // 设置tableView的数据源
    _tableView.dataSource = self;
    // 设置tableView的委托
    _tableView.delegate = self;
    //禁止滚动
    _tableView.scrollEnabled = NO;
    //设置约束
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(screenBounds.size.width,240));
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_collectionView.mas_bottom).offset(screenBounds.size.height/100);
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSIndexPath是一个对象，记录了组和行信息
//    NSLog(@"生成单元格(组：%i,行%i)",indexPath.section,indexPath.row);
//    KCContactGroup *group=_contacts[indexPath.section];
//    KCContact *contact=group.contacts[indexPath.row];
    
    //由于此方法调用十分频繁，cell的标示声明成静态变量有利于性能优化
    static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";
    //首先根据标识去缓存池取
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //如果缓存池没有到则重新创建并放到缓存池中
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text= [_cellLabels objectAtIndex:row];
    cell.imageView.image = [UIImage imageNamed:[_cellImages objectAtIndex:row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

//返回分区数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

//设置高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


@end
